package com.yanxu.test;

import com.utils.Const;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.consumer.ConsumeFromWhere;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

/**
 * @author 折戟沉沙铁未销
 * @version V1.0
 * @date 2024/3/31-2024
 * @Description: 这里用一句话描述这个类的作用
 */
public class BaseConsumer {
    public static void main(String[] args) throws Exception {
        //定义 consumer
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("cooqe-consumerabc");

        //设置 NameServer 的地址
        consumer.setNamesrvAddr(Const.NAME_SERVER_SINGLE);

        //从队列第一个消息开始进行消费
        consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);


        //订阅第一个主题下消息
        consumer.subscribe("cooqe-test","*");
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                System.out.println("----------------开始进行消费!!!");
                for (MessageExt messageExt : list) {
                    System.out.println(new String(messageExt.getBody()));
                    System.out.println("------------------------");
                }
                System.out.println("消息全部消费完毕了!!!");
                //return ConsumeConcurrentlyStatus.RECONSUME_LATER;
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });

        consumer.start();
        Thread.sleep(1000000000);
    }
}